#!/usr/bin/env python
# -*- coding: utf-8 -*
import os
from Enums import FileType
from FileLoader import FileLoader
from FileParser import FileParser
from FilesInfo import FilesInfo
from PrintTool import PrintTool


# 文件解析过程流水线
class FilesPipLine:
    dir_path = './'  # 路径地址
    file_type = FileType.C
    print_tool = PrintTool()  # 打印工具
    file_parser = FileParser()  # 文件解析类
    files_info = FilesInfo()  # 数据统计
    file_loader = FileLoader()  # 文件加载类

    def __init__(self, dir_path, file_type):
        self.dir_path = dir_path
        self.file_type = file_type

    # 根据提供目录, 遍历所有文件, 将符合条件的文件进行统计
    def start_print(self, file_type):
        for root, dirs, file in os.walk(self.dir_path, topdown=False):
            for path in file:
                # 根据类型进行文件过滤
                if self.file_type == FileType.C:
                    if not path.endswith('.cpp') and not path.endswith('.c') and not path.endswith('.cc'):
                        continue
                elif self.file_type == FileType.Ruby:
                    if not path.endswith('.rb'):
                        continue
                # 符合的文件进行统计, 并加入到整体统计中
                self.file_loader.get_file(os.path.join(root, path),
                                          lambda fo: self.files_info.add(
                                              self.file_parser.parse_file_info(fo, self.file_type)))

        # 将统计到的数据输出到屏幕
        self.print_tool.print_by_infos(['Files', 'Lines', 'Code', 'Comments', 'Blanks'],
                                       [self.files_info.files, self.files_info.lines, self.files_info.code,
                                        self.files_info.comments,
                                        self.files_info.blanks])

        # self.files_info.save(file_type)
